from board import Board
import pygame, sys, time, random


def random_coord():
    return (random.randint(0, 4), random.randint(0, 6))
    
def mover(y_distance, x_distance):
    def move_dot(dot, board):
        board.dot_flip(dot)
        dot = (dot[0]+y_distance, dot[1]+x_distance)
        board.dot_flip(dot)
        return dot
    return move_dot
                        
def main():
    score = 0

    me = (2, 3)
    apple = [a for a in [random_coord() for _ in range(0, 10)] if a != me][0]
    myboard = Board('5x7 Board', (152, 110))
    myboard.dot_flip(me)
    myboard.dot_flip(apple)
    cdir = "RIGHT"
    move = {
        "UP" : mover(-1, 0),
        "DOWN" : mover(1, 0),
        "LEFT" : mover(0, -1),
        "RIGHT" : mover(0, 1)
    }
    
    delay = 550
    
    pygame.time.set_timer(pygame.locals.USEREVENT+1, delay)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.locals.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.locals.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    cdir = "DOWN"
                elif event.key == pygame.K_RIGHT:
                    cdir = "RIGHT"
                elif event.key == pygame.K_LEFT:
                    cdir = "LEFT"
                elif event.key == pygame.K_UP:
                    cdir = "UP"
            elif event.type == pygame.locals.USEREVENT+1:
                me = move[cdir](me, myboard)
                if me == apple:
                    newapple = apple
                    while(newapple == apple):
                        newapple = random_coord()
                    apple = newapple
                    myboard.dot_flip(me)
                    myboard.dot_flip(apple)
                    score += 1
                    print score
                    if score % 3 == 0 and score < 12:
                        delay -= 100
                        pygame.time.set_timer(pygame.locals.USEREVENT+1, delay)
                if me[0] > 4 or me[1] > 6 or me[0] < 0 or me[1] < 0:
                    myboard.clear()
                    print 'You lose!'
                    # I think THIS is the worst line of code here:
                    pygame.time.set_timer(pygame.locals.USEREVENT+1, 100000000) 
                    if score > 5*7: 
                        score = 5*7
                    x = 0
                    y = 0
                    oldscore = score
                    while(score > 0):
                        myboard.dot_flip((y, x))
                        if x == 6: 
                            x = 0
                            y += 1
                        else:
                            x += 1
                        score -= 1
                        time.sleep(0.1)
                    if oldscore >= 5*7:
                        print 'Congratulations, you filled up the board!'
                        while True: # supposed to flash the screen, but doesn't?
                            myboard.clear()
                            time.sleep(0.5)
                            myboard.fill()
                            time.sleep(0.5)
    return 0

if __name__ == '__main__': 
    main()
